/**
 * Created by zhout on 2017/6/19.
 */

var index=new Vue({
    el: '#index-body',
    data: {
        notOwnjurisdictionList:[],
        permissionId:'',
        inOwnjurisdictionList:[],
        OwnjurisdictionList:[],
        oldPassword:'',
        newPassword:'',
        confirmPassword:'',
        permissionList:[],
        secondpermissionList:[],
        account: sessionStorage.getItem('account'),
        name: sessionStorage.getItem('name'),
        positionName: sessionStorage.getItem("positionName"),
        teamName: sessionStorage.getItem("teamName"),
        departmentName: sessionStorage.getItem("departmentName")
    },
    methods:{
        /**
         * 获取未拥有权限
         * @constructor
         */
        GetnotOwnjurisdictionList:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/permission/get-not-permission",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId"),
                            positionId:sessionStorage.getItem("positionId")
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                index.notOwnjurisdictionList=data.data;
                            }
                            else if(data.code == 0){
                                alert(data.message);
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }

        },

        /**
         * 获取员工已拥有权限
         * @constructor
         */
        GetinOwnjurisdictionList:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/permission/get-apply-permission",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId")
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                index.inOwnjurisdictionList=data.data;
                            }
                            else if(data.code == 0){
                                alert(data.message);
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }
        },

        /**
         * 获取员工、职位拥有的权限
         * @constructor
         */
        GetOwnjurisdictionList:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/permission/get-permission",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId"),
                            positionId:sessionStorage.getItem("positionId")
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                index.OwnjurisdictionList=data.data;
                            }
                            else if(data.code == 0){
                                alert(data.message);
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }
        },

        /**
         * 选中事件处理
         * @param per
         * @param e
         */
        selectOp:function (per,e) {
            index.permissionId=per.permissionId;
            var event = e.currentTarget;
            $(event).toggleClass("select-option");
            $(".selectoption ul li").each(function () {
                if(this!=event){
                    $(this).removeClass("select-option");
                }
            })
        },

        /**
         * 确认权限申请
         */
        confirmApply:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/permission/permission-application",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId"),
                            permissionId:index.permissionId
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                Ewin.alert("成功申请权限！");
                                $("#permission-application").modal('hide');
                            }
                            else if(data.code == 0){
                                alert(data.message);
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }
        },

        /**
         * 根据员工id、职位id获取二级权限
         * @constructor
         */
        GetSecondpermission:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/permission/level-permission",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId"),
                            positionId:sessionStorage.getItem("positionId"),
                            level:2
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                index.secondpermissionList=data.data;
                            }
                            else if(data.code == 0){
                                alert(data.message);
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }
        },

        /**
         * 修改密码
         * @returns {boolean}
         */
        updatePassword:function () {
            if(isLogin()){
                if(!isLoginLose()){
                    if(!checkIsNull(index.oldPassword)||!checkIsNull(index.newPassword)||!checkIsNull(index.confirmPassword)){
                        return false;
                    }
                    if(index.newPassword.length < 6){
                        Ewin.alert('密码长度不能少于6位！');
                        return false;
                    }
                    if(index.newPassword!=index.confirmPassword){
                        Ewin.alert('密码不一致！');
                        index.newPassword="";
                        index.confirmPassword="";
                        return false;
                    }
                    var config = {
                        // method:"GET",
                        url: httpURL + "/el/employee/update-pwd",
                        params: {
                            employeeId:sessionStorage.getItem("employeeId"),
                            oldPassword:index.oldPassword,
                            newPassword:index.newPassword
                        },
                        onSuccess: function (data) {
                            var data = eval('(' + data + ')');
                            if (data.code == 1) {
                                $("#update-pass-modal").modal('hide');
                                Ewin.alert("您已成功修改密码，请重新登录！").on(function () {
                                    sessionStorage.clear();
                                    window.location.href = "login.html?"+(new Date()).getTime();
                                });
                            }
                            else if(data.code == 0){
                                Ewin.alert("原密码错误！");
                            }
                        }
                    };
                    ajaxHelper().apiReq(config);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }

        },

        /**
         * 菜单点击事件处理
         * @param url
         */
        skip:function (url) {
            if(isLogin()){
                if(!isLoginLose()){
                    $('#content').load(url);
                }else{
                    Ewin.alert('登录过期，请重新登录！').on(function(){
                        sessionStorage.clear();
                        window.location.href="login.html?"+(new Date()).getTime();
                    });
                }
            }else{
                Ewin.alert('未登录，请先登录！').on(function(){
                    window.location.href="login.html?"+(new Date()).getTime();
                });
            }
        }
    }
});

$(function () {
    if(isLogin()){
        if(!isLoginLose()){
            index.permissionList = JSON.parse(sessionStorage.getItem("permission"));
        }else{
            Ewin.alert('登录过期，请重新登录！').on(function(){
                sessionStorage.clear();
                window.location.href="login.html?" + (new Date()).getTime();
            });
        }
    }else{
        Ewin.alert('未登录，请先登录！').on(function(){
            window.location.href="login.html?" + (new Date()).getTime();
        });
    }
});

function loginOut() {
    if(isLogin()){
        if(!isLoginLose()){
            Ewin.confirm({ message: "确认退出吗？" }).on(function (e) {
                if (e) {
                    sessionStorage.clear();
                    window.location.href="login.html?"+(new Date()).getTime();
                }
            });
        }else{
            Ewin.alert('登录过期，请重新登录！').on(function(){
                sessionStorage.clear();
                window.location.href="login.html?"+(new Date()).getTime();
            });
        }
    }else{
        Ewin.alert('未登录，请先登录！').on(function(){
            window.location.href="login.html?"+(new Date()).getTime();
        });
    }
}
function  infomation() {
    $('#content').load("own_info.html");
    $("#personal-center-modal").modal("hide");
}

function  apply() {
    // $("#personal-center-modal").modal('hide');
    index.GetnotOwnjurisdictionList();
    index.GetinOwnjurisdictionList();
    index.GetOwnjurisdictionList();
    $("#permission-application").modal('show');
    setTimeout('$("#personal-center-modal").modal("hide")',500);
}
function updatePassword() {
    $("#update-pass-modal").modal('show');
    setTimeout('$("#personal-center-modal").modal("hide")',500);
}